package cn.itrip.searh.test;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.Field;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

public class ItripHotel implements Serializable {

    @Field
    private String id;

    @Field
    private String hotelName;

    @Field
    private String address;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getHotelName() {
        return hotelName;
    }

    public void setHotelName(String hotelName) {
        this.hotelName = hotelName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "ItripHotel{" +
                "id=" + id +
                ", hotelName='" + hotelName + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    public static void main(String[] args) throws IOException, SolrServerException {
        String url = "http://localhost:8080/solr/hotel/";
        //创建solr客户端对象
        HttpSolrClient httpSolrClient = new HttpSolrClient(url);
        httpSolrClient.setConnectionTimeout(500);

        //创建查询对象
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("hotelName:北京");

        QueryResponse response = httpSolrClient.query(solrQuery);

        List<ItripHotel> list = response.getBeans(ItripHotel.class);

        for (ItripHotel hotel:list){
            System.out.println(hotel.toString());
        }
    }
}
